package com.wyz.saas.common.notifice.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.wyz.saas.common.notifice.AtomDataRegistry
import com.wyz.saas.common.notifice.intent.ReceiverIntent

abstract class AtomBroadcastReceiver<T> : BroadcastReceiver {

    private val actionID: String

    protected constructor(actionID: String) : super() {
        this.actionID = actionID
    }

    fun getActionID(): String {
        return actionID
    }

    protected abstract fun handle(ctx: Context, data: T)


    override fun onReceive(context: Context?, intent: Intent?) {
        val index = intent?.getIntExtra( ReceiverIntent.NAME , -1) ?: -1
        //noinspection unchecked
        val data = AtomDataRegistry.retrieve<T>(index, true)
        handle(context!!, data)
    }
}